/** ###################################################################
**     Filename  : Events.C
**     Project   : project_xgate
**     Processor : MC9S12XDT512BMFU
**     Beantype  : Events
**     Version   : Driver 01.04
**     Compiler  : CodeWarrior HCS12X C Compiler
**     Date/Time : 3/24/2009, 8:51 PM
**     Abstract  :
**         This is user's event module.
**         Put your event handler code here.
**     Settings  :
**     Contents  :
**         No public methods
**
**     (c) Copyright UNIS, a.s. 1997-2008
**     UNIS, a.s.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
** ###################################################################*/
/* MODULE Events */


#include "Cpu.h"
#include "Events.h"
#include "MMA7455.h"
#include "my_types.h"

#pragma CODE_SEG DEFAULT

/*
** ===================================================================
**     Event       :  Inhr1_OnCapture (module Events)
**
**     From bean   :  Inhr1 [Capture]
**     Description :
**         This event is called on capturing of Timer/Counter actual
**         value (only when the bean is enabled - <Enable> and the
**         events are enabled - <EnableEvent>.This event is available
**         only if a <interrupt service/event> is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/



void Inhr1_OnCapture(void)
{
  
  /* Write your code here ... */
}

/*
** ===================================================================
**     Event       :  Inhr1_OnCapture (module Events)
**
**     From bean   :  Inhr1 [Capture]
**     Description :
**         This event is called on capturing of Timer/Counter actual
**         value (only when the bean is enabled - <Enable> and the
**         events are enabled - <EnableEvent>.This event is available
**         only if a <interrupt service/event> is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/

/*void Inhr1_OnCapture(void)
{
 // Inhr1_Process();   
} */

/*
** ===================================================================
**     Event       :  Accel_Y_OnCapture (module Events)
**
**     From bean   :  Accel_Y [Capture]
**     Description :
**         This event is called on capturing of Timer/Counter actual
**         value (only when the bean is enabled - <Enable> and the
**         events are enabled - <EnableEvent>.This event is available
**         only if a <interrupt service/event> is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void Accel_Y_OnCapture(void)
{
//  Accel_Y_Process();
}
  
  

 

/*
** ===================================================================
**     Event       :  Tim100Hz_OnInterrupt (module Events)
**
**     From bean   :  Tim100Hz [TimerInt]
**     Description :
**         When a timer interrupt occurs this event is called (only
**         when the bean is enabled - <Enable> and the events are
**         enabled - <EnableEvent>). This event is enabled only if a
**         <interrupt service/event> is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
#include "integer.h"
#include "diskio.h"
extern BYTE Timer1, Timer2;
extern WORD Timer;
extern volatile DSTATUS Stat;
extern volatile BYTE ignition_switch_val;

void Tim100Hz_OnInterrupt(void)
{
    BYTE n;
    static BYTE cd_pv,ig_pv;
  	BYTE cd_p,ig_p,s;
    Timer++;
  n = Timer1;                                             /* 100Hz decrement timer */
  if (n) Timer1 = --n;
  n = Timer2;
  if (n) Timer2 = --n;
  
  	cd_p = cd_pv;
  	cd_pv = SD_CD_GetVal();	// Sample socket switch 

ignition_switch_val= Ignition_GetVal();

	if (cd_p == cd_pv) {					// Have contacts stabled? 
		s = Stat;

		if (cd_p)			// INS = H (Socket empty) 
			s |= (STA_NODISK | STA_NOINIT);
		else						// INS = L (Card inserted) 
			s &= ~STA_NODISK;
		
      s &= ~STA_PROTECT;
		Stat = s;
	}  
 

}


/*
** ===================================================================
**     Event       :  RPM_OnCapture (module Events)
**
**     From bean   :  RPM [Capture]
**     Description :
**         This event is called on capturing of Timer/Counter actual
**         value (only when the bean is enabled - <Enable> and the
**         events are enabled - <EnableEvent>.This event is available
**         only if a <interrupt service/event> is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
#include "Tach.h"
void RPM_OnCapture(void)
{
  Tach_Process();
  /* Write your code here ... */
}

/*
** ===================================================================
**     Event       :  GEAR_OnEnd (module Events)
**
**     From bean   :  GEAR [ADC]
**     Description :
**         This event is called after the measurement (which consists
**         of <1 or more conversions>) is/are finished.
**         The event is available only when the <Interrupt
**         service/event> property is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/

word Gear_V;
bool NewGear = 0;
void GEAR_OnEnd(void)
{
  GEAR_GetValue16(&Gear_V);
  
  NewGear = 1;
 /* Write your code here ... */
}

/*
** ===================================================================
**     Event       :  ADC_Trig_OnInterrupt (module Events)
**
**     From bean   :  ADC_Trig [TimerInt]
**     Description :
**         When a timer interrupt occurs this event is called (only
**         when the bean is enabled - <Enable> and the events are
**         enabled - <EnableEvent>). This event is enabled only if a
**         <interrupt service/event> is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void ADC_Trig_OnInterrupt(void)
{
  /* Write your code here ... */
}


/*
** ===================================================================
**     Event       :  DisplayUpd_OnInterrupt (module Events)
**
**     From bean   :  DisplayUpd [TimerInt]
**     Description :
**         When a timer interrupt occurs this event is called (only
**         when the bean is enabled - <Enable> and the events are
**         enabled - <EnableEvent>). This event is enabled only if a
**         <interrupt service/event> is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/

extern char display_data;
void DisplayUpd_OnInterrupt(void)
{
  display_data = 1;  

    

}



/*
** ===================================================================
**     Event       :  INT1_CAP_OnCapture (module Events)
**
**     From bean   :  INT1_CAP [Capture]
**     Description :
**         This event is called on capturing of Timer/Counter actual
**         value (only when the bean is enabled - <Enable> and the
**         events are enabled - <EnableEvent>.This event is available
**         only if a <interrupt service/event> is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/

extern signed char X,Y,Z;                              //Ion MMA7455L acceleration data (8Bits)
signed char oldx[12];
signed char oldy[12];
signed char oldz[12];
byte i;
void INT1_CAP_OnCapture(void)
{

  signed short sumx,sumy,sumz;
  /* Write your code here ... */
    for(i=1;i<12;i++){ 
      oldx[i-1]=oldx[i]; 
      oldy[i-1]=oldy[i]; 
      oldz[i-1]=oldz[i]; 
    }
    oldx[11]=   MMA7455_readReg(XOUT8);
    oldy[11]=  MMA7455_readReg(YOUT8);
    oldz[11]=  MMA7455_readReg(ZOUT8);
    
    sumx = sumy = sumz = 0;
    for(i=0;i<12;i++){
        sumx +=oldx[i];
        sumy +=oldy[i];
        sumz +=oldz[i];  
    }
    X = sumx/12;                         
    Y = sumy/12;                           
    Z = sumz/12;                             
  
}

/*
** ===================================================================
**     Event       :  FUNCTION_SW_OnCapture (module Events)
**
**     From bean   :  FUNCTION_SW [Capture]
**     Description :
**         This event is called on capturing of Timer/Counter actual
**         value (only when the bean is enabled - <Enable> and the
**         events are enabled - <EnableEvent>.This event is available
**         only if a <interrupt service/event> is enabled.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/

extern byte tach_state;
void FUNCTION_SW_OnCapture(void)
{
  /* Write your code here ... */
  Diag_LED_NegVal();
  tach_state = ~tach_state; 

}

/* END Events */

/*
** ###################################################################
**
**     This file was created by UNIS Processor Expert 2.99 [04.12]
**     for the Freescale HCS12X series of microcontrollers.
**
** ###################################################################
*/
